//
//  UFOEpisodesViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOEpisodesViewController.h"
#import "UFOEpisodesTableViewCell.h"
#import "LQCAFNetTool.h"
#import "UFOEpisodesModel.h"
#import "UFOWebViewController.h"
@interface UFOEpisodesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *typeArr;
@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UILabel *infoL;
@end

@implementation UFOEpisodesViewController
- (void)dealloc
{
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [_tableV release];
    [_typeArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(85 * SCREENWIDTH / WIDTHSIXP, 30 * SCREENHEIGHT / HEIGHTSIXP, 200 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP)];
    label.text = self.storyName;
    label.font = [UIFont systemFontOfSize:20.0];
    label.textColor = [UIColor purpleColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0 * SCREENWIDTH / WIDTHSIXP, 30 * SCREENHEIGHT / HEIGHTSIXP, 50 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [button setImage:[UIImage imageNamed:@"fanhui232"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [self createTableView];
    
    [self getData];
}

- (void)buttonAction
{

    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)createTableView
{

    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 50) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor colorWithRed:0.390 green:0.173 blue:0.601 alpha:0.780];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
}

- (void)getData
{
    
    self.typeArr = [NSMutableArray array];
    NSString *url =  [NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/novels?catwordid=%@&catid=10195&page=1&i_=801844CE-87DA-42D5-B028-286599ACDF87&t_=1453350832&p_=4841&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol",self.tagL];
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"data"];
        self.typeArr = [UFOEpisodesModel baseModelWithArr:arr];
        NSLog(@"%@",self.typeArr);
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (0 == indexPath.row) {
        return 100;
    } else {
        return 100;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UFOWebViewController *webVC = [[UFOWebViewController alloc] init];
    UFOEpisodesModel *model = [self.typeArr objectAtIndex:indexPath.row];
    webVC.html = model.item_id;
    [self presentViewController:webVC animated:YES completion:^{
        
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.typeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.row) {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.backgroundColor = [UIColor whiteColor];
        self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 90, 90)];
        [self.imageV sd_setImageWithURL:[NSURL URLWithString:self.storyImageV] placeholderImage:nil];
        [cell addSubview:self.imageV];
        
        self.label = [[UILabel alloc] initWithFrame:CGRectMake(110, 0 , SCREENWIDTH - 10 , 30)];
        self.label.text = self.storyName;
        self.label.textColor = [UIColor whiteColor];
        self.label.font = [UIFont systemFontOfSize:18.0];
        cell.backgroundColor = [UIColor colorWithRed:0.482 green:0.237 blue:0.822 alpha:0.710];
        [cell addSubview:self.label];
        
        self.infoL = [[UILabel alloc] initWithFrame:CGRectMake(110, 30, SCREENWIDTH - 140, 60)];
        self.infoL.text = self.storyIntro;
        self.infoL.textColor = [UIColor colorWithRed:1.000 green:0.966 blue:0.560 alpha:1.000];
        self.infoL.font = [UIFont systemFontOfSize:15.0];
        self.infoL.numberOfLines = 0;
        [cell addSubview:self.infoL];
        return cell;
    } else {
    
        static NSString *cellStr = @"cell";
        UFOEpisodesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (nil == cell) {
            cell = [[UFOEpisodesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellStr];
        }
        UFOEpisodesModel *model = [self.typeArr objectAtIndex:indexPath.row];
        cell.titleL.text = model.title;
        cell.introduceL.text = model.desc;
        cell.backgroundColor = [UIColor colorWithRed:0.482 green:0.237 blue:0.822 alpha:0.710];
        return cell;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
